import os, json

class Worker(object):
    def __init__(self):
        print("tooles.baidu.worker.init()")
        self.cmd_run = """
        curl 'https://ai.baidu.com/aidemo' \
          -s -H 'Accept: */*' \
          -H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8' \
          -H 'Cache-Control: no-cache' \
          -H 'Connection: keep-alive' \
          -H 'Content-Type: application/x-www-form-urlencoded' \
          -H 'Cookie: BAIDUID=180964B453EDE1A08CCE675C3D7F4DD0:FG=1; PSTM=1712911286; BIDUPSID=593045737A7F34AFD54EB09836DF1D25; BDUSS=F3TzA4SVZ5LWVwbEt0RmhGUmdrVEdaR041V2xaUms2QTVMQ3VxOC1-S3dtRTFtSVFBQUFBJCQAAAAAAAAAAAEAAACttT8Dd3d3c2VjAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALALJmawCyZmQ0; BDUSS_BFESS=F3TzA4SVZ5LWVwbEt0RmhGUmdrVEdaR041V2xaUms2QTVMQ3VxOC1-S3dtRTFtSVFBQUFBJCQAAAAAAAAAAAEAAACttT8Dd3d3c2VjAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALALJmawCyZmQ0; newlogin=1; H_WISE_SIDS=40300_40446_40080_60137_40463_60220; H_PS_PSSID=40300_40446_40080_60137_40463_60220; H_WISE_SIDS_BFESS=40300_40446_40080_60137_40463_60220; BAIDUID_BFESS=180964B453EDE1A08CCE675C3D7F4DD0:FG=1; ZFY=ul7ej0ybrGCiaA8RPciXDJ9fFcK1tS97nHp:Bpdn1:BJc:C; ET_WHITELIST=etwhitelistintwodays; BA_HECTOR=050024a1aga0a18ga025a585j4bhrp1j3p5h31s; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDRCVFR[Gm3feW4UKgs]=mbxnW11j9Dfmh7GuZR8mvqV; delPer=0; PSINO=1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; Hm_lvt_8b973192450250dd85b9011320b455ba=1713323445,1713429653,1714905084,1715313736; bce-ctl-client-cookies=; SIGNIN_UC=; bce-passport-stoken=; __cas__rn__=; bce-sessionid=; bce-user-info=; bce-ctl-sessionmfa-cookie=; ucbearer-token=; bce-session=; bce-verify-status=; CAMPAIGN_TRACK_TIME=; __cas__st__285=; ucbearer-ucid=; bce-login-display-name=; CAMPAIGN_TRACK=; bce-userbind-source=; ucbearer-clientid=; bce-login-userid=; bce-device-token=; bce-auth-type=; __cas__id__285=; bce-device-cuid=; bce-login-type=; bce-login-expire-time=; ucbearer-devicecode=; Hm_lpvt_8b973192450250dd85b9011320b455ba=1715314104; ab_sr=1.0.1_MDJiZWQ5YTllMDhmNmY1MWIyMTRkMzFiZTdkOWY4MGQyOWJmY2ZiOWMzODBiYWFhNDczZGQzNDIxMzBmMWYzOTYyOGQ3YWFiMWMwZDgzMjliN2EwYTk2ZDc5NzE0ZTc0ZDA1ODllMjRkZThmYWViNWY3MmI3NTE3ZGZhZDFlNTRjYTQ5ZDZlYzk3NzM5MzA3NDYxYmQwNDE2NWZkNjUxZg==' \
          -H 'Origin: https://ai.baidu.com' \
          -H 'Pragma: no-cache' \
          -H 'Referer: https://ai.baidu.com/tech' \
          -H 'Sec-Fetch-Dest: empty' \
          -H 'Sec-Fetch-Mode: cors' \
          -H 'Sec-Fetch-Site: same-origin' \
          -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36' \
          -H 'sec-ch-ua: "Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"' \
          -H 'sec-ch-ua-mobile: ?0' \
          -H 'sec-ch-ua-platform: "macOS"' \
          --data-raw 'image&image_url={image_url}&type={type}&show={show}&detect_direction=false&language_type=CHN_ENG'
        """
        self.types = {
            'advanced_general': {'type': 'advanced_general', 'show': 'true'},
            'plant': {'type': 'advanced_general', 'show': 'true'},
            'animal': {'type': 'advanced_general', 'show': 'true'},
            'cat': {'type': 'advanced_general', 'show': 'true'},
            'dish': {'type': 'advanced_general', 'show': 'true'},
            'landmark': {'type': 'advanced_general', 'show': 'true'},
            'ingredient': {'type': 'advanced_general', 'show': 'true'},
            'object_detect': {'type': 'advanced_general', 'show': 'true'},
            'body_seg':  {'type': 'body_seg', 'show': ''},
            'gesture': {'type': 'gesture', 'show': ''},
            'body_analysis': {'type': 'body_analysis', 'show': ''},
            'fingertip': {'type': 'https://aip.baidubce.com/rest/2.0/image-classify/v1/fingertip', 'show': ''},
            'accurate_basic': {'type': 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic', 'show': ''},
            'general_basic': {'type': 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic', 'show': ''},
            'accurate': {'type': 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate', 'show': ''},
            'general': {'type': 'https://aip.baidubce.com/rest/2.0/ocr/v1/general', 'show': ''},

        }
        # self.types = {
        #     'selfie_anime': 'https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime', # 人像动漫化
        #     'colourize': 'colourize', #上色
        #     'style_trans': 'https://aip.baidubce.com/rest/2.0/image-process/v1/style_trans', # 风格化 self.options
        #     'image_definition_enhance':'https://aip.baidubce.com/rest/2.0/image-process/v1/image_definition_enhance', #清晰度
        #     'img_quality_enhance': 'img_quality_enhance', # 无损放大
        #     'body_seg': 'body_seg', # 人像分割，去背景
        #     'body_analysis':'body_analysis', # 人体关键点识别
        #     'gesture':'gesture', # 手势识别
        #     'fingertip':'https://aip.baidubce.com/rest/2.0/image-classify/v1/fingertip', #指尖检测
        #     'advanced_general': 'advanced_general', # 通用识别 baike_num: 1
        #     'plant':'plant', # 植物 show=true
        # }
        self.options = ["cartoon", "pencil", "color_pencil", "warm", "wave", "lavender", "mononoke", "scream", "gothic"]
        #通用识别 advanced_general, plant, animal, cat, dish, landmark, ingredient, object_detect


    def proc(self, typ, input):
        print("tooles.baidu.proc")
        parms = self.types.get(typ)
        parms["image_url"] = input
        cmd1 = self.cmd_run.format(**parms)
        lines = os.popen(cmd1).readlines()
        res0 = json.loads("".join(lines))
        # print(res0)
        if res0["errno"] == 0:
            result =  res0.get("data")
            return result
            # return f'{result.get("root")}/{result.get("keyword")}'
        else:
            return res0.msg

if __name__ == '__main__':
    worker = Worker()
    # res = worker.proc('gesture', 'https://audio.nndou.com/static/test2.jpg')
    # print(res)
    # res = worker.proc('plant', 'https://audio.nndou.com/static/test2.jpg')
    # print(res)
    # res = worker.proc('body_analysis', 'https://audio.nndou.com/static/test2.jpg')
    # print(res)
    # res = worker.proc('body_seg', 'https://audio.nndou.com/static/test2.jpg')
    # print(res)
    res = worker.proc('accurate', 'https://audio.nndou.com/static/test.jpg')
    print(res)


